package com.ujs.controller;

import com.ujs.pojo.Authority;
import com.ujs.pojo.Order;
import com.ujs.pojo.Role;
import com.ujs.pojo.UserInfo;
import com.ujs.service.RoleService;
import com.ujs.service.UserInfoService;
import com.ujs.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;


    @RequestMapping("/findAll")
    public String findAllByPage(Model model, @RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
                                @RequestParam(name = "pageSize", required = true, defaultValue = "4") Integer pageSize) {
        List<Role> list = roleService.findAll();
        PageUtil<Role> pageInfo = new PageUtil<>(page, pageSize, list);
        List<Role> roleList = pageInfo.getList();
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("roleList", roleList);
        return "role-list";
    }

    @RequestMapping("/role-add")
    public String roleAdd() {
        return "role-add";
    }



    @RequestMapping("/save")
    public String save(Role role) {
        roleService.save(role);
        return "redirect:findAll";
    }

    @RequestMapping("/toShowRole")
    public String toShowRole(Model model,String id) {
        Role role = roleService.findById(id);
        model.addAttribute("role", role);
        return "role-show";
    }
    @RequestMapping("/findRoleByIdAndAllAuthority")
    public String findRoleByIdAndAllAuthority(Model model
            ,@RequestParam(name = "id",required = true) String id) {
        Role role = roleService.findById(id);
        List<Authority>authorityList= roleService.findOtherAuthority(id);
        model.addAttribute("role",role);
        model.addAttribute("authorityList", authorityList);
        return "role-authority";
    }


    @RequestMapping("/addAuthorityToRole")
    public String addRoleToUser(@RequestParam(name="id",required = true)String id,
                                @RequestParam(name="ids",required = true)String []authorityIds){
        roleService.addAuthorityToRole(id,authorityIds);
        return "redirect:findAll";
    }


}
